import ZIM from '@/js_sdk/zego-ZIMUniplugin-JS/lib/ZIM.js';
import { getZegoToken } from "@/api/login.js"
import { getUserInfo } from '@/api/my.js'

export default {

	/**
	 * 即构登录(有反馈)
	 */
	zegoLogin() {
		var that = this
		return new Promise(async (resolve, reject) => {
			const res1 = await getUserInfo({})
			if (res1.code == 1) {
				//获取zimToken
				let params = {
					user_id: res1.data.id
				}
				let res2 = await getZegoToken(params);
				if (res2.code == 1) {
					var userInfo = {
						userID: res1.data.id + "",
						userName: res1.data.nickname
					};
					ZIM.getInstance().login(userInfo, res2.data.token)
						.then(function () {
							console.log('即购登录成功');
							resolve()
						})
						.catch(function (err) {
							console.log('即购登录失败', err);
							setTimeout(() => {
								that.zegoLogin()
							}, 200)
							ZIM.getInstance().logout()
						});
				} else {
					reject("zegoToken获取失败:" + res2.msg)
				}
			} else {
				reject("登录失败:" + res1.msg)
			}
		})
	}
}